package com.woniu108.valueAdded.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.carService.commos.dto.TicketDto;
import com.woniu108.carService.commos.dto.UserInfoDto;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.valueAdded.mapper.TicketMapper;
import com.woniu108.valueAdded.model.Ticket;
import com.woniu108.valueAdded.model.VipLevelForm;
import com.woniu108.valueAdded.param.TicketListParam;
import com.woniu108.valueAdded.service.TicketService;
import com.woniu108.valueAdded.service.VipLevelFormService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 优惠券表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class TicketServiceImpl extends ServiceImpl<TicketMapper, Ticket> implements TicketService {

    @Resource
    private TicketMapper ticketMapper;
    @Resource
    private VipLevelFormService vipLevelFormService;
    @Override
    public Page<TicketDto> ticketList(TicketListParam param) {
        QueryWrapper<Ticket> wrapper = new QueryWrapper<>();
        if(param.getFloorLimit()!=null) wrapper.ge("cut_down_price",param.getFloorLimit());
        if(param.getUpLimit()!=null) wrapper.le("cut_down_price",param.getUpLimit());
        wrapper.ne("ticket_state", StaticData.TICKET_STATE_DELETE);
        List<Ticket> ticketList = ticketMapper.selectList(wrapper);
        List<TicketDto> dtos = BeanUtil.copyToList(ticketList, TicketDto.class);
        long total = dtos.size();
        long pages=total%param.getPageSize()==0?total/param.getPageSize():total/param.getPageSize()+1;
        Page<TicketDto> page=new Page<TicketDto>();
        List<TicketDto> list =new ArrayList<>();
        int i=0;
        for (TicketDto ticketDto : dtos) {
            if(i<param.getPageNum()*param.getPageSize().intValue()&&i>=(param.getPageNum()-1)*param.getPageSize().intValue()){
                QueryWrapper<VipLevelForm> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("vip_level",ticketDto.getAllowVipLevel());
                VipLevelForm levelForm = vipLevelFormService.getOne(wrapper1);
                ticketDto.setAllowVipName(levelForm.getVipLevelName());
                list.add(ticketDto);
            }
            i=i+1;
        }
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        return page;
    }
}
